<?php
/**
 * Page gestion dela saisie des salles
 * Permet d'imprimer une liste de salles vides
 * et de les compléter
 *
 * @category actions
 * @package administration
 *
 * @author M.CASTANHEIRO, M.MEUNIER, O.PATTE, C.PORTEMER, R.THERY-HERMAIN
 * @version 1.0
 */
class saisiesalleAction extends shAction {
    /**
     * Fonction appellée automatiquement pour toutes les requêtes de l'action
     */
    public function init() {
	$this->setTitle('Saisie des salles');
	$this->addAriane(shRequest::link('administration'), 'Administration');
    }

    /**
     * Execute la méthode par défaut pour l'action salle
     */
    public function execute($_date=null, $_promotions=null) {
	$date = (!empty($_date)) ? Date::fromURL($_date) : Date::today();
	$promotions = (!empty($_promotions)) ? explode('-',$_promotions) : array();

	// Création du formulaire
	$formulaire = new shFormulaire;

	shField::date('Date')
		->setValue($date->toFormulaire())
		->appendTo($formulaire);

	$multibox_promotion = shField::multibox('Promotions')
		->setValue($promotions)
		->appendTo($formulaire);

	$s_promotions = shSGBD::select(
		'SELECT promotion.id, specialite.intitule AS specialite, filiere.intitule AS filiere, promotion.date_debut, promotion.date_fin '.
		'FROM promotion '.
		'INNER JOIN specialite ON promotion.id_specialite=specialite.id '.
		'INNER JOIN filiere ON specialite.id_filiere=filiere.id '.
		'WHERE promotion.date_debut<=:date '.
		'AND promotion.date_fin>=:date '.
		'ORDER BY filiere.intitule, specialite.intitule ',
		array(':date' => Date::today()->toSGBD() )
	);

	foreach ( $s_promotions AS $promotion ) {
	    $annee = Date::fromSGBD($promotion->date_debut)->format('Y') .'-'. Date::fromSGBD($promotion->date_fin)->format('Y');
	    $multibox_promotion->addOption($promotion->id, $promotion->specialite.' '.$promotion->filiere.' '.$annee);
	}

	if ( empty($promotions) AND !empty($s_promotions) ) {
	    $promotions[] = $s_promotions[0]->id;
	    $multibox_promotion->setValue(array($promotions[0]));
	}

	$formulaire->setSubmit(function($formulaire) {
		    shAction::redirect(
			    'administration', 'saisiesalle', null,
			    array(Date::fromFormulaire($formulaire->getField('Date')->getValue())->toURL(), implode('-', $formulaire->getField('Promotions')->getValue()))
		    );
		})


	;

	// Récupère les salles
	$salles = shSGBD::select(
		'SELECT salle.id, salle.intitule '.
		'FROM salle '.
		'INNER JOIN batiment ON salle.id_batiment=batiment.id '.
		'ORDER BY batiment.intitule, salle.intitule'
	);

	$this->assign('formulaire', $formulaire);
	$this->assign('promotions', $this->executePrint($date, implode('-', $promotions)));
	$this->assign('promos', implode('-', $promotions));
	$this->assign('salles', $salles);
	$this->assign('date', $date->toURL());
    }

    /**
     * Crée un pdf avec la liste des promotions et les séances vides
     * @param <array> promotions
     */
    public function executePrint($date, $promotions=null) {
	if ( !empty($promotions) ) {
	    $promotions = str_replace('-',',', $promotions);

	    $promos = shSGBD::select(
		    'SELECT promotion.id, specialite.intitule AS specialite, filiere.intitule AS filiere '.
		    'FROM promotion '.
		    'INNER JOIN specialite ON promotion.id_specialite=specialite.id '.
		    'INNER JOIN filiere ON specialite.id_filiere=filiere.id '.
		    'WHERE promotion.id IN ('.$promotions.') '.
		    'ORDER BY filiere.intitule, specialite.intitule '
	    );

	    $mut = array();
	    foreach ( $promos AS &$promo ) {
		$promo->titre = $promo->specialite.' '.$promo->filiere;
		$promo->seances = shSGBD::select (
			'SELECT ue.intitule AS ue, enseignement.id AS id_enseignement, type_enseignement.intitule AS type_enseignement, '.
			'seance.jour, seance.heure_debut, '.
			'seance.heure_fin, seance.id '.
			'FROM seance '.
			'INNER JOIN enseignement ON seance.id_enseignement=enseignement.id '.
			'INNER JOIN ue ON enseignement.id_ue=ue.id '.
			'INNER JOIN type_enseignement ON enseignement.id_type_enseignement=type_enseignement.id '.
			'INNER JOIN mutualiser ON seance.id=mutualiser.id_seance '.
			'WHERE (seance.id_salle IS NULL OR seance.id_salle="") '.
			'AND mutualiser.id_promotion=? '.
			'ORDER BY seance.jour, seance.heure_debut, seance.heure_fin ',
			array($promo->id)
		);

		foreach($promo->seances AS $key=>&$seance) {
		    // Si mutualisé ?
		    $mutualises = shSGBD::select(
			    'SELECT promotion.id, specialite.intitule AS specialite, filiere.intitule AS filiere '.
			    'FROM promotion '.
			    'INNER JOIN specialite ON promotion.id_specialite=specialite.id '.
			    'INNER JOIN filiere ON specialite.id_filiere=filiere.id '.
			    'INNER JOIN mutualiser ON promotion.id=mutualiser.id_promotion '.
			    'WHERE mutualiser.id_seance=? ',
			    array($seance->id));

		    if ( sizeof($mutualises) > 1 ) {
			$ids_promotions = array();
			$promot = new stdClass();
			$promot->id = 0;
			$promot->titre = '';
			foreach ( $mutualises AS $mutualise ) {
			    $ids_promotions[] = $mutualise->id;
			    $promot->titre .=  $mutualise->specialite.' '.$mutualise->filiere . ' & ';
			}

			$promot->titre = substr($promot->titre, 0, -3);

			sort($ids_promotions);
			$ids_promotions = implode('-', $ids_promotions);

			if ( !isset($promos[$ids_promotions])) {
			    $mut[$ids_promotions] = $promot;
			}

			if ( !isset($promos[$ids_promotions]->seances ) ) {
			    $mut[$ids_promotions]->seances = array();
			}

			$mut[$ids_promotions]->seances[$seance->id] = $seance;
			unset($promo->seances[$key]);

		    }
		}
	    }
	    $promos = array_merge($promos, $mut);
	}
	else {
	    $promos = array();
	}
	$this->assign('promotions', $promos);
	return $promos;
    }

    /**
     * Rempli les champs
     * @param <array> Promotions
     */
    public function executeEdit($date, $promotions) {
	$formulaire = new shFormulaire;
	$formulaire->setSubmitText('Valider les salles');
	$multi = shField::multibox('salle')
		->appendTo($formulaire);

	// Récupère les salles
	$salles = shSGBD::select(
		'SELECT salle.id, salle.intitule '.
		'FROM salle '.
		'INNER JOIN batiment ON salle.id_batiment=batiment.id '.
		'ORDER BY batiment.intitule, salle.intitule'
	);

	foreach($salles AS $salle) {
	    $multi->addOption($salle->id, $salle->intitule);
	}


	$formulaire->execute();
	$salles = $formulaire->getField('salle')->getValue();

	foreach ( $salles AS $seance=>$salle) {
	    shSGBD::update(
		    'UPDATE seance '.
		    'SET id_salle=:salle '.
		    'WHERE id=:id ',
		    array(':salle'=>$salle, ':id'=>$seance)
	    );
	}

	shAction::redirect('administration', 'saisiesalle', null);

    }
}


?>